using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace Wsp365.Anonymous.Features.Moderation
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("cb13f2a3-debc-49af-ac08-536897cde51d")]
    public class ModerationEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            if (web.WebTemplate == "BLOG")
            {
                SPList list = web.GetListByWebRelativeUrl("Lists/Comments");
                list.EnableModeration = true;
                list.DraftVersionVisibility = DraftVisibilityType.Approver;
                list.AnonymousPermMask64 =
                    SPBasePermissions.ManagePermissions |
                    SPBasePermissions.Open |
                    SPBasePermissions.OpenItems |
                    SPBasePermissions.ViewFormPages |
                    SPBasePermissions.ViewListItems |
                    SPBasePermissions.AddListItems;            
                if (!list.RootFolder.Properties.ContainsKey(Wsp365.Anonymous.ModerationEventReceiver.ModerationEventReceiver.PropertyName))
                {
                    list.RootFolder.AddProperty(Wsp365.Anonymous.ModerationEventReceiver.ModerationEventReceiver.PropertyName,
                        Wsp365.Anonymous.ModerationEventReceiver.ModerationEventReceiver.Enabled);
                    list.RootFolder.Update();
                }
                list.Update();
            }
        }


        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            if (web.WebTemplate == "BLOG")
            {
                SPList list = web.GetListByWebRelativeUrl("Lists/Comments");
                list.EnableModeration = false;
                list.AnonymousPermMask64 =
                    SPBasePermissions.Open |
                    SPBasePermissions.OpenItems |
                    SPBasePermissions.ViewFormPages |
                    SPBasePermissions.ViewListItems |
                    SPBasePermissions.AddListItems;
                if (list.RootFolder.Properties.ContainsKey(Wsp365.Anonymous.ModerationEventReceiver.ModerationEventReceiver.PropertyName))
                {
                    list.RootFolder.DeleteProperty(Wsp365.Anonymous.ModerationEventReceiver.ModerationEventReceiver.PropertyName);
                    list.RootFolder.Update();
                }
                list.Update();
            }
        }
    }
}
